#include <iostream>
#include <ctime>
#include "AL_ThreadPoolManager.h"
#include "TestTask.h"
#include "DependencyList.h"

void Task_One(float _fDelta);
void Task_Two(float _fDelta);
void Task_Three(float _fDelta);
void Task_Four(float _fDelta);


#define NUM_TASKS 4

int main(void)
{
	srand(unsigned int(time(0)));

	AL_ThreadPoolManager* pPool = AL_ThreadPoolManager::GetInstance();
	pPool->Init();
	pPool->CreateThreadPool(3);
	/*Task_One(10);
	Task_Two(10);
	Task_Three(10);
	Task_Four(10);*/

	TestTask* Tasks[NUM_TASKS];
	typedef void (*Func)(float);
	Func TaskFuncs[4] = {Task_One, Task_Two, Task_Three, Task_Four};
	for(int i=0 ; i<NUM_TASKS;i++)
	{
		Tasks[i]=new TestTask(i);
		Tasks[i]->SetTask(TaskFuncs[i]);
	}

	/*Tasks[0]->SetTask(Task_One);
	Tasks[1]->SetTask(Task_Two);
	Tasks[2]->SetTask(Task_Three);
	Tasks[3]->SetTask(Task_Four);*/

	for(int i=0;i<NUM_TASKS;i++)
		pPool->SendTask(Tasks[i]);

	while(true)
	{
		pPool->UpdatePool(0.0f);

		int rNum = rand()%40;
		if(rNum<=3)
		{
			TestTask* tmp = new TestTask(rNum);
			tmp->SetTask(TaskFuncs[rNum]);
			pPool->SendTask(tmp);
		}
		if(pPool->GetNumDormentThreads() == pPool->GetTotalThreads())
			break;
	}

	pPool->Shutdown();

	system("pause");
	return 0;
}

void Task_One(float _fDelta)
{
	printf("Task One: Starting\n");
	for(int i=0;i<1000;i++);
	printf("Task One: Finished\n");
}

void Task_Two(float _fDelta)
{
	printf("Task Two: Starting\n");
	for(int i=0;i<200000;i++);	
	printf("Task Two: Finished\n");
}

void Task_Three(float _fDelta)
{
	printf("Task Three: Starting\n");
	for(int i=0;i<5000;i++);
	printf("Task Three: Finished\n");
}

void Task_Four(float _fDelta)
{
	printf("Task Four: Starting\n");
	for(int i=0;i<1500;i++);
	printf("Task Four: Finished\n");
}